package com.yannan.lock.core.util;

import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.*;


public class CommonUtil {

	/**
	 * 对象转map
	 * @param obj 需要转换的对象
	 * @param excludeValues 需要排除的值
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> objectToMap(Object obj, String... excludeValues) throws Exception {    
        if(obj == null){    
            return null;    
        }   
        Map<String, Object> map = new HashMap<String, Object>();    
        Field[] declaredFields = obj.getClass().getDeclaredFields();    
        for (Field field : declaredFields) {
        	if(isExist(excludeValues, field.getName())){
        		continue;
        	}
            field.setAccessible(true);  
            if(field.get(obj) instanceof Date){
            	map.put(field.getName(), DateUtil.getStringDate((Date)field.get(obj)));  
            }else{
            	map.put(field.getName(), field.get(obj));  
            }
        }    
        return map;  
    }
	
	public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
        if (map == null)  
            return null;    
  
        Object obj = beanClass.newInstance();  
  
        Field[] fields = obj.getClass().getDeclaredFields();   
        for (Field field : fields) {    
            int mod = field.getModifiers();    
            if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){    
                continue;    
            }    
  
            field.setAccessible(true);    
            field.set(obj, map.get(field.getName()));   
        }   
  
        return obj;    
    }
	
	public static boolean isExist(String[] arr, String targetValue) {
		    for (String s : arr) {
		        if (s.equals(targetValue)) {
		            return true;
		        }
		    }
		    return false;
		}

	/**
	 * 取对象的属性值
	 * @param obj
	 * @param propName
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	public static Object getPropertyValue(Object obj, String propName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		return PropertyUtils.getProperty(obj, propName);
	}
	
	/**
	 * 拷贝非空属性(排除)
	 * @param source
	 * @param
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void copyProps(Object source, Object target, String...ignorProps) {
		if(source instanceof Map){
			try {
				Map sourceMap = (Map)source;
				Set<String> ignorPropsSet = new HashSet<String>();
				if(null!=ignorProps){
					for(String prop : ignorProps){
						ignorPropsSet.add(prop);
					}
				}
				
				for(Object key : sourceMap.keySet()){
					if(null==key || ignorPropsSet.contains(key)){
						continue;
					}else{
						Object value = sourceMap.get(key);
						if(target instanceof Map){
							Map targetMap = (Map)target;
							targetMap.put(key, value);
						}else{
							if(PropertyUtils.isWriteable(target, key.toString())){
								PropertyUtils.setProperty(target, key.toString(), value);
							}
						}
					}
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}else{
			org.springframework.beans.BeanUtils.copyProperties(source, target, ignorProps);
		}
	}
}
